<?php

namespace App\Models;

use App\Internals\CourseInternal;
use Carbon\Carbon;

class UserCourse extends Model
{
    const IS_END = [
        'no'    => 0, // 未结束
        'push'  => 1, // 推送结束
        'learn' => 2, // 学习结束
    ];

    protected $table = 'user_courses';

    // ============= scope ==========
    public function scopeOfUserCourse($query, $userId, $courseId)
    {
        return $query->where('user_id', $userId)->where('course_id', $courseId);
    }

    public function scopeOfUserBuyCourse($query, $userId, $courseId)
    {
        return $query->where('user_id', $userId)->where('course_id', $courseId)->where('status', static::IS_TRUE);
    }


    public function scopeOfUserBuyLevel($query, $userId, $level)
    {
        return $query->where('user_id', $userId)->where('level', $level)->where('status', static::IS_TRUE);
    }


    // ============= field ==========

    public function getCourseId()
    {
        return $this->course_id;
    }

    public function setCourseId($courseId)
    {
        $this->course_id = $courseId;
    }

    public function getLevel()
    {
        return $this->level;
    }

    public function setLevel($level)
    {
        $this->level = $level;
    }

    public function getUserId()
    {
        return $this->user_id;
    }

    public function setUserId($userId)
    {
        $this->user_id = $userId;
    }

    public function setOrderId($orderId)
    {
        $this->order_id = $orderId;
    }

    public function getLessonCount()
    {
        return $this->lesson_count;
    }

    public function getWordCount()
    {
        return $this->word_count;
    }

    public function getDubCount()
    {
        return $this->dub_count;
    }

    public function getStudyDateCount()
    {
        return  Carbon::parse(date('Y-m-d', strtotime($this->end_time)))->diffInDays($this->start_time) + 1;
    }

    public function getScore()
    {
        return $this->score;
    }

    public function setScore($score)
    {
        $this->score = $score;
    }

    public function setStar($star)
    {
        $this->star = $star;
    }

    public function getStar()
    {
        return $this->star;
    }

    public function getIsCurrent()
    {
        return $this->is_current;
    }

    public function setIsCurrent($status)
    {
        $this->is_current = $status;
    }

    public function setShared($status)
    {
        $this->is_shared = $status;
    }

    public function getIsEnd()
    {
        return $this->is_end;
    }

    public function setIsEnd($isEnd)
    {
        $this->is_end = $isEnd;
    }

    public function getStartTime()
    {
        return $this->start_time;
    }

    // 购买时间算作开始学习时间
    public function setStartTime($time)
    {
        $this->start_time = $time;
    }

    public function setEndTime($time)
    {
        $this->end_time = $time;
    }

    public function setStatus($status)
    {
        $this->status = $status;
    }

    public function getStatus()
    {
        return $this->status;
    }

    public static function getCurrent($userId)
    {
        $courseObj = static::query()->where('user_id', $userId)->where('is_current', static::IS_TRUE)->first();
        if (!$courseObj) {
            // 购买之后的
            /** @var UserCourse $courseObj */
            $courseObj = static::query()->where('user_id', $userId)->where('status', static::IS_TRUE)->where('is_end', '!=', static::IS_END['learn'])->orderBy('level', 'asc')->first();
            if ($courseObj) {
                $courseObj->setIsCurrent(static::IS_TRUE);
                $courseObj->setStartTime(date('Y-m-d H:i:s'));
                $courseObj->save();
                // 设置首次学习时间
                /** @var UserStatus $userStatus */
                $userStatus = UserStatus::where('user_id', $userId)->first();
                $userStatus->setStartStudyTime(date('Y-m-d H:i:s'));
                $userStatus->save();
            }
        }
        return $courseObj;
    }

    public static function getAllUsersCurrent($isEnd, $page=0, $limit=100)
    {
        $model = static::query()->where('status', static::IS_TRUE)->where('is_current', static::IS_TRUE);
        if (!$isEnd) {
            $model->where('is_end', static::IS_END['no']);
        }

        if ($page) {
            $model->forPage($page, $limit);
        }

        return $model->get();
    }

    public static function getNextCourse($userId, $currentLevel)
    {
        $result = [
            'nextLevel' => null,
            'isBuy' => false,
            'nextCourseId' => null
        ];
        if ($currentLevel != Level::MAX_LEVEL) {
            $nextLevel = $currentLevel + 1;
            $nextLevelCourse = CourseInternal::getLevelCourse($nextLevel);
            $isBuy =  static::query()->where('user_id', $userId)->where('level', $nextLevel)->where('status', static::IS_TRUE)->orderBy('level', 'asc')->exists();
            $result = [
                'nextLevel' => $nextLevel,
                'nextCourseId' => $nextLevelCourse['id'],
                'isBuy' => $isBuy,
            ];
        }
        return $result;
    }

}
